//
//  CountryCodeSelectViewController.m
//  TenMinDemo
//
//  Created by Shawn Li on 2021/3/16.
//  Copyright © 2021 CYXiang. All rights reserved.
//

#import "CountryCodeSelectViewController.h"
#import "BaseTextField.h"
#import "CCodeCell.h"

@interface CountryCodeSelectViewController ()<UITextFieldDelegate,UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet BaseTextField *seachInput;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
/// <#注释#>
@property (nonatomic, strong) NSArray *countryList;

/// <#注释#>
@property (nonatomic, strong) NSArray *allList;

/// <#注释#>
@property (nonatomic, copy) NSString *keyword;
@end

@implementation CountryCodeSelectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getcountryCode];
    
}

- (void)getcountryCode{
    self.countryList = @[];
    self.allList = @[];
    NSError *error = nil;
    NSString *textFileContents = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"countrycode1" ofType:@"txt"] encoding:NSUTF8StringEncoding error:&error];
    if (error) {
        LRLog(@"move failed:%@", [error localizedDescription]);
        return;
    }
    
    NSData *data = [textFileContents dataUsingEncoding:NSUTF8StringEncoding];
    //再将data转成字典
    id res = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
    if (error) {
        LRLog(@"move failed:%@", [error localizedDescription]);
        return;
    }
    self.allList = res;
    self.countryList = res;
    LRLog(@"%@", res);
    
}

- (void)setupUI{
    [super setupUI];
    self.seachInput.placeholder = [NSString stringWithFormat:@"%@..",LCLocalString(@"ssgjdq")];
    self.seachInput.placeholderColor = [UIColor colorWithHexString:@"#4d5273"];
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 36, 36)];
    UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake(12, 10, 16, 16)];
    [leftView addSubview:imgV];
    //    imgV.contentMode = UIViewContentModeCenter;
    imgV.image = [UIImage imageNamed:@"ss_icon"];
    self.seachInput.leftView = leftView;
    self.seachInput.leftViewMode=UITextFieldViewModeAlways;
    LRViewBorderRadius(self.seachInput, 5, 0, LRClearColor);
    
    self.tableView.isNeedEmpty = @"1";
    [self.tableView registerNib:[UINib nibWithNibName:@"CCodeCell" bundle:nil] forCellReuseIdentifier:@"CCodeCell"];
}
- (IBAction)cancelAction:(id)sender {
    [self.view endEditing:YES];
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.countryList.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *info = self.countryList[indexPath.row];
    CCodeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CCodeCell"];
    
    [cell refreshWith:info];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *info = self.countryList[indexPath.row];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (self.backBlock) {
        self.backBlock(info);
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark- 搜索

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    [self performSelector:@selector(doSearch) withObject:nil afterDelay:0.05];
    return YES;
}


- (void)doSearch{
    self.keyword = self.seachInput.text;
    if (!self.allList.count) {
        return;
    }
    if(!self.keyword || !self.keyword.length){
        self.countryList = self.allList;
    }else{
        NSMutableArray *temp = @[].mutableCopy;
        for (NSDictionary *info in self.allList) {
            NSString *name = info[@"name"];
            if([name containsString:self.keyword]){
                [temp addObject:info];
            }
        }
        self.countryList = temp;
        
    }
    
    [self.tableView reloadData];
}

@end
